package musicstore.com.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import musicstore.com.dao.BaseDao;
import musicstore.com.dao.UserDao;
import musicstore.com.entity.User;

public class UserDaoImpl extends BaseDao implements UserDao{

/*	public boolean addUser(User user){
		Connection conn = null ;
		PreparedStatement stmt = null ;
		int result = 0 ;

		try {
			conn = openConnetion();
			String sql = "INSERT INTO USER (Username,`Password`,`Name`,Address,Phone ,Email ,UserRoleId ,IsInUse) VALUES(?,?,?,?,?,?,?,?)" ;
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, user.getUserName());
			stmt.setString(2, user.getPassword());
			stmt.setString(3, user.getName());
			stmt.setString(4, user.getAddress());
			stmt.setString(5, user.getPhone());
			stmt.setString(6, user.getEmail());
			stmt.setInt(7, user.getUserRoleId());
			stmt.setInt(8, user.getIsInUse());
			result = stmt.executeUpdate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeConnection(conn, stmt, null);
		}
		
		return result > 0;
	}*/
	
/*
	public boolean checkUser(String username) {
		Connection conn = null ;
		PreparedStatement stmt = null ;
		ResultSet rs = null;
		
		try {
			conn = openConnetion();
			String sql = "select * from `user` where username = ?";
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, username);
			rs = stmt.executeQuery();
			if(rs.next()){
				return true;
			}
				
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeConnection(conn, stmt, rs);
		}
		return false;
	}*/

	public boolean addUser(User user) {
		this.openConnection();
		String sql = "INSERT INTO USER VALUES(0,?,?,?,?,?,?,1,1)" ;
		ArrayList list = new ArrayList();
		list.add(user.getUserName());
		list.add(user.getPassword());
		list.add(user.getName());
		list.add(user.getAddress());
		list.add(user.getPhone());
		list.add(user.getEmail());
		int rows = 0;
		
		try {
			rows = executeUpdate(sql, list.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeResource();
		}
		return rows > 0;
	}

	public boolean checkUser(String username) {
		String sql = "select * from `user` where username = ?";
		ResultSet rs = null;
		ArrayList list = new ArrayList();
		list.add(username);
		
		this.openConnection();
		try {
			rs = executeQuery(sql, list.toArray());
			if(rs.next())
				return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeResource();
		}
		
		return false;
	}

	public User checkUser(String username, String password) {
		User user = null ;
		String sql ="select * from `user` where `username`=? and `password`=?";
		this.openConnection();
		try {
			rs = executeQuery(sql, new Object[]{username,password});
			if(rs.next()){
				int id = rs.getInt("id") ;
				String name = rs.getString("name");
				String address = rs.getString("address");
				String phone = rs.getString("phone");
				String email = rs.getString("email");
				int userRoleId = rs.getInt("userroleid");
				int isInUse = rs.getInt("isinuse");
				user = new User(id, username, password, name, address, phone, email, userRoleId, isInUse);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeResource();
		}
		return user;
	}

	public static void main(String[] args) {
		User user = new User(0, "hjjj", "000000", "jsd", "bakdjsbjakbkj", "12345654378", "jiamin200988@164.com", 1, 1);
		boolean result = new UserDaoImpl().addUser(user);
		System.out.println(result);
	}
}
